package com.gobuy.system.service.impl;

import com.gobuy.system.dao.AdminMapper;
import com.gobuy.system.pojo.Admin;
import com.gobuy.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Gaozexi on 2020/6/4 11:35
 * Step by Step  and Stand on,  You Are The Best Investment!
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 新增管理员
     * @param admin
     */
    @Override
    public void add(Admin admin) {
        //获取盐
        String gensalt = BCrypt.gensalt();
        //对用户的密码进行加密
        String hashpw = BCrypt.hashpw(admin.getPassword(), gensalt);
        admin.setPassword(hashpw);
        adminMapper.insert(admin);
    }

    /**
     * 用户登录
     * @param admin
     * @return
     */
    @Override
    public boolean login(Admin admin) {
        //根据登录名获取管理员信息
        Admin admin1 = new Admin();
        admin1.setLoginName(admin.getLoginName());
        admin1.setStatus("1");
        Admin adminResult = adminMapper.selectOne(admin1);
        if(adminResult == null){
            return false;
        }else {
            //校验密码
            return BCrypt.checkpw(admin.getPassword(),adminResult.getPassword());
        }
    }

    /**
     * 查询所有管理员信息
     * @return
     */
    @Override
    public List<Admin> findAll() {
        return adminMapper.selectAll();
    }

}
